import asyncio

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.processor.yzy.yzy_school_img_processor import YZYSchoolImgProcessor
from com.arcfox.school.spider.yzy.yzy_base_spider import YZYBaseSpider
from com.arcfox.util import async_request as requests
from com.arcfox.util.redis_key_manager import YZY_UNIVERSITY_SCHOOL_LIST_KEY
from com.arcfox.util.util import random_sleep


class YZYSchoolImgSpider(YZYBaseSpider):
    def __init__(self):
        super().__init__()
        self.task_manager = RedisTaskManager(YZY_UNIVERSITY_SCHOOL_LIST_KEY)
        self.url = f"https://uwf7de983aad7a717eb.youzy.cn/youzy.dms.basiclib.api.college.image.get"
        self.processor = YZYSchoolImgProcessor()

    def get_params(self, college_code):
        return {
            'collegeCode': college_code,
        }

    def get_headers(self, college_code):
        return {
            "Host": "uwf7de983aad7a717eb.youzy.cn",
            "u-sign": self.get_u_sgin(f"collegeCode={college_code}"),
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
            "Referer": "https://pv4y-pc.youzy.cn/",
        }

    async def _pull_task(self):
        return await self.task_manager.pull_tasks(10), ExecuteType.FINISH

    async def _crawl_by_task(self, tasks):
        for task in tasks:
            params = self.get_params(task['yzy_code'])
            resp = await requests.post_with_session(self.url, data=params, headers=self.get_headers(task['yzy_code']),
                                                    timeout=5)
            if resp.code == 200:
                result_json = resp.json()
                img_list = self.processor.parse_all_img(result_json)
                for img in img_list:
                    try:
                        img_resp = await requests.get_with_session(img['url'], headers=self.get_default_header(), timeout=5)
                        await self.processor.save_img(task, img, img_resp.response)
                    except Exception as e:
                        pass
                    await random_sleep(0.2)


if __name__ == "__main__":
    asyncio.run(YZYSchoolImgSpider().start_crawl())
